package com.dalianpai.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author WGR
 * @create 2020/7/7 -- 19:34
 */
public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    //    public static void main(String[] args) {
//        System.out.println("main......start");
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).whenCompleteAsync((t, e) -> {
//            System.out.println("结果是：" + t + "异常:" + e);
//        }).exceptionally(throwable -> {
//            return 10;
//        });
//        Integer integer = future.get();
//        System.out.println("main......end");
//    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hellp.jpg";
        }, executor);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        }, executor);
        CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        }, executor);
//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        allOf.get();  //阻塞
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        Object o = anyOf.get();
        System.out.println("main......end"+o);
    }
}
